home *** CD-ROM | disk | FTP | other *** search
- Path: gail.ripco.com!mambuhl
- From: mambuhl@ripco.com (Martin Ambuhl)
- Newsgroups: comp.lang.c
- Subject: Re: C beginner needs your
- Date: 19 Feb 1996 18:53:52 GMT
- Organization: Ripco Communications, Inc.
- Message-ID: <4gah00$6c6@gail.ripco.com>
- NNTP-Posting-Host: foley.ripco.com
-
- sebag@ecs.umass.edu
- in <4g862f$p0b@risky.ecs.umass.edu> asks:
-
- >I am a new C programmmer who desperately needs help.
- >I have been digging in the manuals for a way to do this but have still
- >come out empty handed. Here is the problem: I want to open files in a while
- >loop with different filenames. data0,data1,data2,data3, .. data1000 , ...
- >I need to increment an integer each time around then convert it to a string
- >and then somehow use strcat to combine the "data" with the integer string.
- >After that use fopen(filename, "a");
-
- One way -- even though it uses neither `while' nor `strcpy' -- follows.
- If it is necessary to use those, you should be able to figure out how to
- change the code:
-
- #include <stdio.h>
- #include <stdlib.h>
-
- int main()
- {
- char filename[FILENAME_MAX];
- FILE *fp;
- int i;
- for (i = 0; /* continuation condition */ ; i++)
- {
- sprintf(filename,"data%d",i);
- if (!(fp = fopen(filename,"a"))) {
- fprintf(stderr,"Could not open file %s\n",filename);
- exit(EXIT_FAILURE);
- }
- /* processing */
- fclose(fp);
- }
- return 0;
- }
-
- --
- * Martin Ambuhl net: mambuhl@ripco.com
- * Chicago, IL (USA)
-